package org.example.rmsmsapi.Utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

/**用于核酸检测的的utilis
 * Created by 黄启位 on 2021-12-12.
 */
public class NapersonUtils {
    /**
     * 获取汉字串拼音首字母，英文字符不变
     * @param chinese 汉字串
     * @return 汉语拼音首字母
     */
    public static String getFirstSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (temp != null) {
                        pybf.append(temp[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().replaceAll("\\W", "").trim();
    }
    /**获取门诊挂号的表明**/
    public static String getMzghTableName(){
    String tabename="";
    tabename="YXHIS"+DateUtils.getCurrentYear()+".."+"TBMZGHMX"+DateUtils.getCurrentYear()+YwxhUtils.Getxh("00",DateUtils.getSysMoth());
    return tabename;

    }
/***获取门诊病历基本信息的表名***/
    public  static String getMzbljzxxbName(String mzh){
        String str="";
        str=mzh.substring(mzh.length()-1,mzh.length());
        String reStr="";
        reStr="YSGZZ"+DateUtils.getCurrentYear()+".."+"TBMZBLJZXX_0"+str;
        return reStr;
    }
/***获取门诊时间的表名**/
    public static String GetMzEvetName(String mzh){
        String str="";
        str=mzh.substring(mzh.length()-1,mzh.length());
        String reStr="";
        reStr="YXHIS"+DateUtils.getCurrentYear()+".."+"TBEVENTMZ_0"+str;
        return reStr;
    }
    /**获取门诊检验申请单信息表名**/
    public static String GetTBMZJYSQDXXWZXName(String mzh){
        String str="";
        str=mzh.substring(mzh.length()-1);
        String reStr="";
        reStr="YSGZZ..TBMZJYSQDXXWZX_0"+str;
        return reStr;
    }

    /**获取门诊检验16个基本节点的表名**/
    public static String GetTBMZJY16name(String mzh){
        String str="";
        str=mzh.substring(mzh.length()-1,mzh.length());
        String reStr="";
        reStr="YSGZZ..TBMZJYSQDMXWZX_0"+str;
        return reStr;
    }
//获取检验申请单的项目名表
    public static String getJYsqxmName(String mzh){
        String str="";
        str=mzh.substring(mzh.length()-1,mzh.length());
        String reStr="";
        reStr="YSGZZ..TBMZJYSQDXMWZX_0"+str;
        return reStr;
    }


    /**
     * 将对象直接转换成String类型的 XML输出
     *
     * @param obj
     * @return
     */
    public static String convertToXml(Object obj) {
        // 创建输出流
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }

}
